//
//  EeAudioDiskView.swift
//  Aiwei
//
//  Created by zhu xietong on 2018/1/17.
//  Copyright © 2018年 zhu xietong. All rights reserved.
//

import Foundation
import Eelay


public class AudioPalyingMedia {
    public var rotation:CGFloat = 0
    public var status:EeRotationStatus = EeRotationStatus.end
    public var url:String = ""
}


public protocol AudioDiskObserver {
    func observerDisk(url:String,degree:CGFloat,status: EeRotationStatus,complete:CGFloat)
}



open class SubAudioPlayer:JoView,AudioDiskObserver{
    
    let imageView = UIImageView()
    let playPauseButton = UIButton()
    var item:EeAudioItem = EeAudioItem(url: "", cover: "", name: "")
    
    public func observerDisk(url: String, degree: CGFloat, status: EeRotationStatus, complete: CGFloat) {
        self.currentPlayPercentage = complete

        if url == item.url{
            self.status = status
            self.corner = degree
            switch status{
            case .end,.pause:
                playPauseButton.isSelected = false
            case .rotationing:
                playPauseButton.isSelected = true
            }
        }
        else{
            corner = 0
            playPauseButton.isSelected = false
        }
    }
    
    var corner:CGFloat = 0{
        didSet{
            imageView.transform = CGAffineTransform(rotationAngle: corner)
        }
    }
    
    var isCurrent:Bool{
        var __isCurrent = false
        if let url = EeAudioPlayer.shared.jukebox.currentItem?.URL.absoluteString
        {
            if url == self.item.url
            {
                __isCurrent = true
            }
        }
        return __isCurrent
    }
    var status = EeRotationStatus.end{
        didSet{
            
        }
    }
    
    var currentPlayPercentage:CGFloat = 0.0
    override open func addLayoutRules() {
        weak var wself = self
        EeAudioDiskView.global.observers.append(wself)
        eelay = [
            [imageView,[ee.X.Y],"140",140],
            [playPauseButton,[ee.X.Y],140,"140"],
        ]
    
        playPauseButton.contentEdgeInsets = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)
        imageView.ui.holder(type: .grayBack).set(radius: 70, width: TP.separatorHeight, color: UIColor(shex: "#9999"))
        let nimg = UIImage(named: "audio_play", in: Bundle(for: EeAudioPlayer.self), compatibleWith: nil)
        let simg = UIImage(named: "audio_pause", in: Bundle(for: EeAudioPlayer.self), compatibleWith: nil)

        playPauseButton.ui.nimage(nimg).simage(simg)
        playPauseButton.addTarget(self, action: #selector(playPauseAction), for: .touchUpInside)
    }
    
    override open func mo_model_reload() {
        status = .end
        imageView.img_url = mo.model["thumb",""]
        
    }
    
    
    @objc open func playPauseAction() {
        if mo.model["needLoad","YES"] == "NO"
        {
            if isCurrent
            {
                if EeAudioPlayer.shared.currentPlayPercentage >= 1{
                    EeAudioPlayer.shared.play(item: item)
                }
                else{
                    EeAudioPlayer.shared.playPauseAction()
                }
            }
            else{
                EeAudioPlayer.shared.play(item: item)
            }
        }
        else{
            item.url = mo.model["file_url",""]
            item.cover = mo.model["thumb",""]
            let _ = EeAudioManager.shared.add(item)
            mo.model["needLoad"] = "NO"
            weak var wself = self
            co_delay(delay: 0.1) {
                guard let ws = wself else{
                    return
                }
                
                EeAudioPlayer.shared.play(item: ws.item)
            }
            
            
//            mo.model["needLoad"] = "NO"
        }
        
       
    }
   
}

open class EeAudioDiskView: EeRotationView {
    
    public static var _global:EeAudioDiskView? = nil

    public static var global:EeAudioDiskView{
        get{
            guard let g = EeAudioDiskView._global else{
                let _g = EeAudioDiskView()
                EeAudioDiskView._global = _g
                return _g
            }
            return g
        }
    }
    
    public var observers:[AudioDiskObserver?] = [AudioDiskObserver?]()
    
    public var media_url:String = ""
    
    public var rotationBlock:(CGFloat,EeRotationStatus)->Void = {_,_ in}
    
    open override func updateDegree(degree: CGFloat, status: EeRotationStatus) {
        rotationBlock(degree,status)
    }
    
    public let imgV = UIImageView()
    open override func addLayoutRules() {
        super.addLayoutRules()
        let shawdowV = UIView()
        contentView.eelay = [
            [imgV,[ee.X.Y],"100",100],
            [shawdowV,[ee.X.Y],"100",100],
        ]
        shawdowV.bsui.set(radius: 50, width: nil, color: nil)
        shawdowV.backgroundColor = UIColor(shex:"#4442")
        imgV.ui.holder(type: .grayBack).set(radius: 50, width: 4, color: UIColor(shex:"#fff9"))
        backgroundColor = .clear
        contentView.backgroundColor = .clear
    }
}
